﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using LevelEditor.Objects;

namespace LevelEditor.Components
{
    public class ZoomPanel : Panel
    {
        private const float ZOOM = 1.25f;
        private const float MINZOOM = 0.1f;
        private float normalZoom;

        private float currentWheelRotation;

        private float currentZoom;
        public float CurrentZoom
        {
            get { return currentZoom; }
            private set 
            {
                currentZoom = value;
                Zoom(currentZoom);
            }
        }

        public ZoomPanel()
            : base()
        {
            this.Click += ZoomPanel_Click;
            this.MouseWheel += ZoomPanel_MouseWheel;
            this.currentZoom = 1;
            this.normalZoom = 400;
        }


        protected virtual void ZoomPanel_MouseWheel(object sender, MouseEventArgs e)
        {
            
            if (e.Delta > 0)
                currentWheelRotation += 100;
            else if (e.Delta < 0) 
                currentWheelRotation -= 100;

            float nextZoom = (currentWheelRotation + this.normalZoom) / normalZoom;


            this.CurrentZoom = nextZoom;
        }

        void ZoomPanel_Click(object sender, EventArgs e)
        {
            this.Focus();
        }

        public void Zoom(float amount)
        {
            for (int k = 0; k < this.Controls.Count; k++)
            {
                Tile tile = this.Controls[k] as Tile;
                if (tile == null)
                    continue;
                this.Controls[k].Size = new Size((int)( ((Tile)this.Controls[k]).Image.Width * currentZoom ), 
                    (int)( ((Tile)this.Controls[k]).Image.Height * currentZoom ) );
                
            }
        }

    }
}
